package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrdersettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrdersettingDao ordersettingDao;
    @Autowired
    private MemberDao memberDao;


    //用户在线体验预约
    @Override
    public Result order(Map map) throws Exception {
        //判断用户所选择的预约日期是否有预约设置,如果没有设置无法预约
        String orderDate = (String) map.get("orderDate");//获取用户要预约的日期
        //获取数据库内设置的预约日期
        OrderSetting byOrderDate = ordersettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        //指定日期没有进行预约设置,无法完成体检预约
        if (byOrderDate == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //检查用户所选择的预约日期是否已经约满
        int number = byOrderDate.getNumber();//可预约人数
        int reservations = byOrderDate.getReservations();//已预约人数
        //判断预约人数是否已满
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //判断用户是否在同一天重复预约一个套餐
        String telephone = (String) map.get("telephone");//获取用户输入的手机号
        Member member = memberDao.findByTelephone(telephone);//获取数据库内用户手机号
        //判断是否已有预约
        if (member != null) {
            Integer id = member.getId();//会员ID
            Date date = DateUtils.parseString2Date(orderDate);//预约日期
            String setmealId = (String) map.get("setmealId");//套餐ID
            //封装
            Order order = new Order(id, date, Integer.parseInt(setmealId));
            //根据条件查询
            List<Order> list = orderDao.findByCondition(order);
            if (list != null && list.size() > 0) {
                //数据库找到数据,说明已预约,返回false
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }

        //判断是否为会员,是的话直接预约,不是自动完成注册并预约
        if (member == null) {
            member = new Member();
            member.setName((String)map.get("name"));
            member.setPhoneNumber((String) map.get(telephone));
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        //预约成功后更新当日的已预约人数
        Order order = new Order();
        //设置会员ID
        order.setMemberId(member.getId());
        //预约日期
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        //预约类型
        order.setOrderType((String) map.get("orderType"));
        //到诊状态
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //套餐ID
        order.setSetmealId((Integer) map.get("setmealId"));
        //更新
        orderDao.add(order);
        //设置已预约人数+1
        byOrderDate.setReservations(byOrderDate.getReservations()+1);
        //编辑
        ordersettingDao.editOrderSettingByDate(byOrderDate);
        //返回成功通知
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    //预约成功页面动态展示
    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if (map != null) {
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
